/*
 * bjm.l
 *
 *  Created on: Jan 12, 2014
 *      Author: Benjamin Groeber, Julian Sanin, Matthaeus Ranigler
 */

%{
#include "nodes.h"
#include "bjm.tab.h"
#include <stdlib.h>
#include <string.h>

// Duplicates the string returning an identical malloc'd string.
char *dublicateString(const char *str);

%}

NUMBER [0-9]
NUMBERS {NUMBER}*
INTNUM "-"?{NUMBER}+
FLOATNUM "-"?{NUMBER}+"."{NUMBER}+

%%

"print"						{ return PRINT; }
"quit"						{ return QUIT; }
"int"	      				{ return INT; }
"float"	     				{ return FLOAT; }

"if"						{ return IF; }
"else"						{ return ELSE; }
"while"						{ return WHILE; }
"until"						{ return UNTIL; }
"{"							{ return STARTSCOPE; }
"}"							{ return ENDSCOPE; }


"and"						{ return AND; }
"&&"						{ return AND; }
"or"						{ return OR; }
"||"						{ return OR; }

"--"						{ return DECR; }
"++"						{ return INCR; }
"<"       					{ return LT; }
"<="      					{ return LE; }
"=="       					{ return EQ; }
"!="       					{ return NE; }
">="       					{ return GE; }
">"      					{ return GT; }

[a-zA-Z]([a-zA-Z]|[0-9])*	{ yylval.sym = dublicateString(yytext); return SYMBOL; }
{FLOATNUM}  				{ yylval.floats = atof(yytext); return FLOATNUM; }
{INTNUM}    				{ yylval.ints = atoi(yytext); return INTNUM; }
[ \t\n]						{ ; }
[-+*=/()!;]					{ return yytext[0]; }
.							{ ECHO; yyerror("Unexpected character"); }

%%

/* Additional C code. */

// Edit this to support multiple files.
int yywrap(void) {
	return 1;
}

char *dublicateString(const char *str) {
	size_t size = strlen(str) + 1;
	void *newStr = malloc(size);
	if(newStr == NULL) {
		return NULL;
	}
	return (char *)memcpy(newStr, str, size);
}

